package com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.DO;

import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.dto.SentinelSystemRuleDTO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import java.io.Serializable;

/**
 * xx
 *
 * @author BugFree Jin
 * @version 1.0
 * @since 2022/3/2 15:48
 */
@Data
@Table(value = "sentinel_system_rule")
public class SentinelSystemRuleDO extends BaseDO {
    private static final long serialVersionUID = -5868380801106513781L;
    @Id
    private long id;

    @Column("system_rule")
    private String systemRule;

    public SentinelSystemRuleDTO getSentinelSystemRuleDTO() {
        SentinelSystemRuleDTO sentinelSystemRuleDTO = new SentinelSystemRuleDTO();
        try {
            SystemRule pfr = new ObjectMapper().readValue(this.systemRule, SystemRule.class);
            BeanUtils.copyProperties(pfr, sentinelSystemRuleDTO);
            BeanUtils.copyProperties(this, sentinelSystemRuleDTO, "systemRule");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return sentinelSystemRuleDTO;
    }

    @Data
    public static class SystemRule implements Serializable {

        private String limitApp;
        private String resource;
        private Long avgRt;
        private Double highestCpuUsage;
        private Long maxThread;
        private Double highestSystemLoad;
        private Double qps;

        private static final long serialVersionUID = 8590547411712322572L;
    }

}
